/**
 * 
 */
package com.scada.wechat.hessian.client;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.ResourceBundle;

import com.caucho.hessian.client.HessianProxyFactory;
import com.scada.wechat.hessian.client.annotation.HessianNamespace;

/**
 * @author Admin1
 *
 */
public abstract class BaseClient<T> {

	protected static HessianProxyFactory factory;
	protected static String SERVICE_URL = "";

	private Class<T> vistorClass;
	private String hessianNamespace;

	static {
		factory = new HessianProxyFactory();
		SERVICE_URL = "http://127.0.0.1:8083/member_server/hessian/";
	}

	@SuppressWarnings("unchecked")
	public BaseClient() {
		HessianNamespace annotation = getClass().getAnnotation(HessianNamespace.class);
		if (annotation != null) {
			hessianNamespace = annotation.value();
		}

		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		vistorClass = (Class<T>) params[0];
	}

	protected HessianProxyFactory getFactory() {
		if (factory == null) {
			synchronized (BaseClient.class) {
				if (factory == null)
					factory = new HessianProxyFactory();
			}
		}
		return factory;
	}

	@SuppressWarnings("unchecked")
	protected T getVistor() {
		T vistor = null;
		try {
			vistor = (T) getFactory().create(vistorClass, SERVICE_URL + hessianNamespace);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return vistor;
	}
}
